package com.nidu.demo.oauth2.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.oauth2.convertor.OAuth2ClientCriteriaConvertor;
import com.nidu.demo.oauth2.dto.OAuth2ClientPageQry;
import com.nidu.demo.oauth2.convertor.OAuth2ClientConvertor;
import com.nidu.demo.oauth2.dto.OAuth2ClientCO;
import com.nidu.demo.oauth2.gateway.OAuth2ClientGateway;
import com.nidu.demo.oauth2.model.OAuth2Client;
import com.nidu.demo.oauth2.model.OAuth2ClientCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RequiredArgsConstructor
public class OAuth2ClientPageQryExe {

    private final OAuth2ClientGateway gateway;

    private final OAuth2ClientConvertor convertor;

    private final OAuth2ClientCriteriaConvertor criteriaConvertor;

    public PageResponse<OAuth2ClientCO> execute(OAuth2ClientPageQry qry) {
        OAuth2ClientCriteria criteria = criteriaConvertor.toDataObject(qry);
        PageResponse<OAuth2Client> page = gateway.pageByCondition(criteria);
        List<OAuth2ClientCO> clientCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(clientCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
